package com.handydata.antifraud.service.impl;

import com.handydata.antifraud.bo.rejectcitybasedata.RejectCityBasedataBeanBO;
import com.handydata.antifraud.bo.rejectcitybasedata.RejectCityBasedataReqBO;
import com.handydata.antifraud.bo.rejectcitybasedata.RejectCityBasedataResBO;
import com.handydata.antifraud.common.exception.RejectCityBasedataExCode;
import com.handydata.antifraud.dao.RejectCityBasedataDao;
import com.handydata.antifraud.entity.RejectCityBasedataDO;
import com.handydata.antifraud.service.RejectCityBasedataService;
import com.handydata.common.convert.CommonBeanConvertor;
import com.handydata.common.exception.SysException;
import com.handydata.common.service.AbsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("rejectCityBasedataService")
public class RejectCityBasedataServiceImpl extends AbsService implements RejectCityBasedataService {
	
	private static final Logger logger = LoggerFactory.getLogger(RejectCityBasedataServiceImpl.class);
	
	@Autowired
	private RejectCityBasedataDao rejectCityBasedataDao;

	@Transactional
	@Override
	public RejectCityBasedataResBO createRejectCityBasedata(RejectCityBasedataReqBO req) throws SysException{
	    RejectCityBasedataResBO res = new RejectCityBasedataResBO();
		try {
			RejectCityBasedataDO entity = CommonBeanConvertor.convert(req, RejectCityBasedataDO.class);
			long status = rejectCityBasedataDao.insert(entity);
			if(status == 0) {
				res.setExCode(RejectCityBasedataExCode.SYS_NO_ENTITY_CREATED);
			} else {
				res.setExCode(RejectCityBasedataExCode.SUCCESS);
			}
 			
		} catch (Exception e) {
			logger.error("Create new entity {} get into error", req, e);
			throw new SysException(RejectCityBasedataExCode.SYS_CREATE_ENTITY_ERROR);
		}
		
		return res;
	}

	@Transactional
	@Override
	public RejectCityBasedataResBO modifyRejectCityBasedata(RejectCityBasedataReqBO req) throws SysException{
	    RejectCityBasedataResBO res = new RejectCityBasedataResBO();
		try {
			RejectCityBasedataDO condition = CommonBeanConvertor.convert(req, RejectCityBasedataDO.class);
			long status = rejectCityBasedataDao.update(condition);
			if(status == 0) {
				res.setExCode(RejectCityBasedataExCode.SYS_NO_ENTITY_MODIFIED);
			} else {
				res.setExCode(RejectCityBasedataExCode.SUCCESS);
			}
		} catch (Exception e) {
			logger.error("Modify entity {} get into error", req, e);
			throw new SysException(RejectCityBasedataExCode.SYS_MODIFY_ENTITY_ERROR);
		} 
		return res;
	}

	@Transactional
	@Override
	public RejectCityBasedataResBO deleteRejectCityBasedata(RejectCityBasedataReqBO req) throws SysException{
	    RejectCityBasedataResBO res = new RejectCityBasedataResBO();
		
		try {
			RejectCityBasedataDO condition = CommonBeanConvertor.convert(req, RejectCityBasedataDO.class);
			long status = rejectCityBasedataDao.delete(condition);
			if(status == 0) {
				res.setExCode(RejectCityBasedataExCode.SYS_NO_ENTITY_DELETED);
			} else {
				res.setExCode(RejectCityBasedataExCode.SUCCESS);
			}
		} catch (Exception e) {
			logger.error("Delete entity {} get into error", req, e);
			throw new SysException(RejectCityBasedataExCode.SYS_DELETE_ENTITY_ERROR);
		}
		return res;
	}

	@Override
	public RejectCityBasedataResBO queryRejectCityBasedata(RejectCityBasedataReqBO req) throws SysException{
		RejectCityBasedataDO condition = CommonBeanConvertor.convert(req, RejectCityBasedataDO.class);
		RejectCityBasedataResBO res;
		
		try {
			List<RejectCityBasedataDO> entities = rejectCityBasedataDao.query(condition);
			res = CommonBeanConvertor.convert(entities, RejectCityBasedataResBO.class, RejectCityBasedataBeanBO.class);
		} catch(Exception e) {
			logger.error("Query entity {} get into error", req, e);
			throw new SysException(RejectCityBasedataExCode.SYS_QUERY_ENTITY_ERROR);
		}
		return res;
	}
	
}
